package collectionDemo;

import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author jie
 * @version 创建时间：2018年11月7日 下午1:53:34
 * push(x) -- 元素 x 入栈
 * pop() -- 移除栈顶元素
 * peek() -- 获取栈顶元素
 * empty() -- 返回栈是否为空
 *
 * iterator statck 顺序
 */
public class StackDemo {
    public static void main(String[] args) {
//        testRemoveAll();
        test1();
    }

    private static void test1() {
        Stack<String> mStack = new Stack<>();
        mStack.push("a");//mStack.add("a");
        mStack.push("b");
        mStack.push("c");


        int a = mStack.search("a");
        System.out.println("StackDemo:test1:search a:"+a);//3  lastIndexOf

        System.out.println("push a b c ==" + mStack);
        for (int i = mStack.size()-1; i >=0; i--) {
            System.out.println("倒叙 "+mStack.get(i));
        }

        Iterator<String> iterator = mStack.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println("iterator "+next);
        }
        // TODO: pop
        String pop = mStack.pop();
        System.out.println("pop == " + pop + " mStack == " + mStack);

        // TODO: peak
        String peek = mStack.peek();
        System.out.println("peek  == " + peek + " mStack ==" + mStack);

        for (String num:mStack) {
            System.out.println("for each mStack == "+num);
        }
        System.err.println("结论：for each 按照 push 顺序遍历，FIFO");

//        Stack<String> clone = (Stack<String>) mStack.clone();
//        System.out.println("clone ==" + clone);
//        Stack<String> tempDel = new Stack<>();
//        tempDel.add(mStack.get(1));
//        tempDel.add(mStack.get(2));
//        mStack.removeAll(tempDel);
//        System.err.println("removeAll==" + mStack);



    }

    private static void testRemoveAll() {
        CopyOnWriteArrayList<String> mStack = new CopyOnWriteArrayList<>();
        mStack.add("a");
        mStack.add("b");
        mStack.add("c");
        CopyOnWriteArrayList<String> tempDel = new CopyOnWriteArrayList<>();
        tempDel.add("a");
        tempDel.add("c");
        tempDel.add("d");
        mStack.removeAll(tempDel);
        for (int i = 0; i < mStack.size(); i++) {
            System.out.println(mStack.get(i));
        }
    }
}
